﻿@model  Model.PageRole
@{
    Layout = "~/Views/Shared/_Main.cshtml";
    ViewBag.Title = "已保存源系统表列表 ";
}
@section Head{
}
<script type="text/javascript">
    var UrlArray = $.getUrlArray();
    var pageRole = '@Model.role';
    function InitList2() {
        $("#DataGrid2").dg({
            rows: 18,
            height: 450,
            url: "/SystemConfiguration/GetSourceViewConfigList?systemID=" + UrlArray["systemID"],
            para: { tableName: $("#txt_TableName").val(), dbName: encodeURI(UrlArray["dbName"]) },
            idField: "Guid",
            checkbox: false,
            rownumbers: true,
            columns: [
                    { field: 'ViewName', title: '表名称', width: 26, sortable: true, formatter: function (value, rowData, rowIndex) {
                        return "<a href='#' onclick='openTBData(\"" + rowData.Guid + "\")'>" + value + "</a>";
                    }
                    },
                    { field: 'DisplayName', title: '显示名称', width: 26, sortable: true },
                    { field: 'ViewChnName', title: '表说明', width: 12, sortable: true },
                    { field: '配置状态', title: '配置状态', width: 8, align: 'center', formatter: function (value, rowData, rowIndex) {
                        if (rowData.ColumnCount == -1) {
                            return "未配置";
                        }
                        else if (rowData.ColumnCount == 0) {
                            return "<font color='#ff0000'>未配置</font>";

                        }
                        if (rowData.ColumnCount > 0) {
                            return "已配置";

                        }
                    }
                    },
                { field: 'StepStatus', title: '任务状态', width: 8, sortable: true, align: 'center',
                    formatter: function (value, rowData, rowIndex) {
                        if (value == "2")
                            return "<img src='/images/stepok.png' />";
                        else
                            return "<img src='/images/steperr.png' />";
                    }
                },
                    { field: 'list_action', title: '操作', width: 10, align: 'center', formatter: function (value, rowData, rowIndex) {
                        var a = "<select onchange='DoAction(this.options.selectedIndex,\"" + rowData.Guid + "\")' style='width: 100px; '><option value=''>-请选择-</option>";
                        if (pageRole == '2')
                            a = a + "<option>配置</option><option>删除</option>";
                        a = a + "<option>查看任务状态</option><option>跟踪</option></select>";
                        return a;
                    }
                    }
                    ]
        });
    }

    //“操作”下拉框事件
    function DoAction(i, value) {
        if (pageRole == '1')
            i = i + 2;
        switch (i) {
            case 1:
                openSourceCB(value);
                break;
            case 2:
                deleteSourceViewConfig(value);
                break;
            case 3:
                ViewETLData(value);
                break;
            case 4:
                GetPath(value, 'source', 0);
                InitList2();
                break;
        }
    }
    // 打开数据源的表字段信息
    function openSourceCB(vcid) {
        var id = UrlArray["systemID"];
        var dbUrl = "";
        if (UrlArray["DBType"] == "2")
            dbUrl += "&DBType=" + UrlArray["DBType"];
        showModalMany('Contact', '/SystemConfiguration/SourceColumnEdit/?systemID=' + id + "&viewID=" + vcid + dbUrl + "&dbName=" + encodeURI(UrlArray["dbName"]), '810', '480');
        InitList2();
    }

    function ViewETLData(id) {
        var dbName = encodeURI(UrlArray["dbName"]);
        var url = "/SystemConfiguration/ViewETLData/?viewID=" + id + "&dbName=" + dbName;
        showModalMany('ViewETLData', url, '810', '430');
        InitList2();
    }
    function openTBData(vcid) {
        var systemID = UrlArray["systemID"];
        var dbName = encodeURI(UrlArray["dbName"]);
        var url = "/SystemConfiguration/SourceLocalTBData/?systemID=" + systemID + "&viewID=" + vcid + "&dbName=" + dbName + "&DBType=" + UrlArray["DBType"];
        showModalMany('Contact', url, '810', '470', 'off');
        InitList2();
    }

    function OnChickRow(v) {
        alert(v);
    }
    function selectAll() {

    }
    function btnSearch() {
        InitList2();
    }

    function AddItem() {
        var url = "/SystemConfiguration/SourceTBList/?systemID=" + UrlArray["systemID"] + "&dbName=" + encodeURI(UrlArray["dbName"]) + "&DBType=" + UrlArray["DBType"];
        showModalMany('Contact', url, '810', '480', onSelectedCallBack);
    }
    function onSelectedCallBack(returnValue) {
        $.post("/SystemConfiguration/AddSourceViewConfig?systemID=" + UrlArray["systemID"] + "&dbName=" + encodeURI(UrlArray["dbName"]),
        { SourceViewConfig: $.toJSON(returnValue), DBType: UrlArray["DBType"] }, function (result) {
            if (result == "1") {
                //alert("连接成功!");
                InitList2();
                return true;
            } else {
                alert("选择数据表失败，请重新选择！");
                InitList2();
                return false;
            }
        });
    }
    function deleteSourceViewConfig(vcid) {
        if (confirm("确定要删除吗？")) {
            if (typeof (vcid) == "undefined" || vcid.length == 0)
                return false;
            $.post("/SystemConfiguration/DeleteSourceViewConfig?systemID=" + UrlArray["systemID"] + "&dbName=" + encodeURI(UrlArray["dbName"]),
            { vcid: vcid }, function (result) {
                if (result.Success) { 
                    InitList2();// 删除成功
                    return true;
                } else {
                    alert(result.Message);
                    InitList2();
                    return false;
                }
            });
        } else
            InitList2();
    }
    function EditView(id) {
        var url = "/ModelConfig/ExtendEdit?modelTableID=" + id + "&dbName=" + encodeURI(UrlArray["dbName"]);
        if (UrlArray["DBType"] == "2")
            url += "&DBType=" + UrlArray["DBType"];
        window.location.href = url;
    }

    function DeleteItem(id) {
        if (confirm("确定要删除吗?")) {
            $.post("/ModelConfig/CheckDelete", { guid: id, dbName: encodeURI(UrlArray["dbName"]) }, function (result) {
                if (result !== "OK") {
                    alert(result);
                } else {
                    $.post("/ModelConfig/Delete", { id: id, dbName: encodeURI(UrlArray["dbName"]) }, function (result) {
                        if (result == 1) {
                            alert("删除成功！");
                            $("#DataGrid").datagrid('reload');
                        }
                    });
                }
            });
        }
    }

    function EditItem(id) {
        var url = "/ModelConfig/Edit?systemID=" + id + "&dbName=" + encodeURI(UrlArray["dbName"]);
        if (UrlArray["DBType"] == "2")
            url += "&DBType=" + UrlArray["DBType"];
        window.location.href = url;
    }
    $(function () {
        InitList2();
        //        getDict();
        //        getModelName();
    });
    function ViewList() {
        InitList();
        $('#mm2-2-1').window('open');
    }
    function InitList() {
        $("#DataGrid").datagrid({
            width: 970,
            height: 420,
            autoRowHeight: false,
            fitColumns: true,
            pagination: true,
            rownumbers: true,
            animate: true,
            singleSelect: true,
            striped: true,
            url: "/ModelConfig/GetList/?modelTypeId=" + encodeURI("") + "&tableName=" + encodeURI("AutoModel") + "&dbName=" + encodeURI(UrlArray["dbName"]),
            idField: "GUID",
            iconCls: 'icon-remove',
            columns: [[
            { field: 'FactTableName', title: '表显示名称', width: 100, formatter: function (value, rowData, rowIndex) {
                return "MdpSelfGet_用户表";
            }
            },
            { field: 'TableName', title: '模型名称', width: 250, formatter: function (value, rowData, rowIndex) {
                if (rowIndex < 5)
                    return "<b>(3/5) </b>" + value;
                else
                    return "( <img src='/images/tree_dnd_yes.png'> ) " + value;
            }
            },
            { field: 'aaa', title: '模型描述', width: 150, formatter: function (value, rowData, rowIndex) {
                return "模型描述";
            }
            },
            { field: 'GUID', title: '操作', width: 80, align: 'center', formatter: function (value, rowData, rowIndex) {
                if (rowIndex < 5) {
                    return ac3;
                } else
                    return ac2;
            }
            }
            ]]
        });
    }

    function getDict() {
        $.post("/ModelConfig/GetMTListByKeyName",
        { keyName: 'ModelTypeName', dbName: encodeURI(UrlArray["dbName"]) },
        function (result) {
            var arr_sel = new Array();
            arr_sel.push("<option value='-1'  >--全部--</option>");
            for (var i = 0; i < result.length; i++) {
                arr_sel.push("<option value='");
                arr_sel.push(result[i].Value);
                arr_sel.push("'>");
                arr_sel.push(result[i].Name);
                arr_sel.push("</option>");
            }
            $("#ddlModelTypeId").append(arr_sel.join(""));
        });
    }
    function getModelName() {
        $.post("/ModelConfig/GetModelTableNameList?dbName=" + encodeURI(UrlArray["dbName"]), function (result) {
            if (result.length > 0) {
                var tablesArray = new Array();
                for (var i = 0; i < result.length; i++) {
                    tablesArray.push({ TableName: result[i] });
                }
                $('#txtTableName').autocomplete(tablesArray, {
                    max: tablesArray.length,    //列表里的条目数
                    minChars: 0,    //自动完成激活之前填入的最小字符
                    width: 395,     //提示的宽度，溢出隐藏
                    scrollHeight: 300,   //提示的高度，溢出显示滚动条
                    matchContains: true,    //包含匹配，就是data参数里的数据，是否只要包含文本框里的数据就显示
                    autoFill: false,    //自动填充
                    formatItem: function (row, i, max) {
                        return row.TableName;
                    },
                    formatMatch: function (row, i, max) {
                        return row.TableName;
                    },
                    formatResult: function (row) {
                        return row.TableName;
                    }
                }).result(function (event, row, formatted) {

                });
            }
        });
    }
    function btnback() {
        var url = "/SystemConfiguration/EtlSourceList?dbName=" + encodeURI(UrlArray["dbName"]);
        if (UrlArray["DBType"] == "2")
            url += "&DBType=" + UrlArray["DBType"];
        window.location.href = url;
    } 

</script>
<div style="margin: 5px;">
    <table class="listTitle">
        <tr>
            <td width="15">
                <img src="/images/filesave.png" />
            </td>
            <td width="300">
                已保存源系统表列表
            </td>
            <td align="right">
                @if (Model.role == 2)
                { 
                    <input type="button" class="button" value=" 添加 " onclick="AddItem()" />
                }
                <input type="button" class="button" value=" 返回 " onclick="btnback()" />
            </td>
        </tr>
    </table>
    <table id="DataGrid2" class="pagTable" cellpadding="0" cellspacing="0">
    </table>
    <div>
        <div id="Pagination" class="pagination">
        </div>
        <div id="loading" class="loading">
            <img src="/Images/tree_loading.gif" />
        </div>
    </div>
    <div id="msg">
    </div>
</div>
